package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private TbBusinessTrackRecordMapper businessTrackRecordMapper;

    @Autowired
    private TbBusinessMapper businessMapper;





    @Override
    public List<TbBusinessTrackRecord> selectTbBusinessTrackRecordList(Long businessId) {
        List<TbBusinessTrackRecord> result = null;
        try {
            result = businessTrackRecordMapper.selectTbBusinessTrackRecordList(businessId);

            result = result.stream().peek(record -> {
                String[] items = record.getKeyItems().split(",");

                //从字典表里查询沟通重点
                List<String> keys = new ArrayList<>();
                for (String item : items) {
                    String key = dictDataMapper.selectDictLabel("communication_point", item);
                    keys.add(key);
                }
                record.setKeys(keys);
                //从字典表里查询跟进状态
                String track_status = dictDataMapper.selectDictLabel("track_status", record.getTrackStatus());
                record.setTrackStatus(track_status);

//                return record;
            }).collect(Collectors.toList());

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    @Override
    public int insertTbBusinessTrackRecord(BusinessTrackVo businessTrackVo) {
        int rows = 0;
        try {
            TbBusiness business = new TbBusiness();
            BeanUtils.copyProperties(businessTrackVo,business);
            business.setId(businessTrackVo.getBusinessId());
            business.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());
            int rows1 = businessMapper.updateTbBusiness(business);

            TbBusinessTrackRecord businessTrackRecord = new TbBusinessTrackRecord();
            BeanUtils.copyProperties(businessTrackVo,businessTrackRecord);

            String username = SecurityUtils.getUsername();

            businessTrackRecord.setCreateBy(username);
            businessTrackRecord.setCreateTime(new Date());
            int rows2 = businessTrackRecordMapper.insertTbBusinessTrackRecord(businessTrackRecord);
            rows = rows1 +rows2;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return rows;
    }
}
